import json
import plistlib

import jsonpath
from testapp.dataio.dataiofactory import DataIOFactory
from testapp.common.getSwaggerapi import GetSwagerApi
from testapp.common.schedulerplan import Schedulerplan
from testapp.common.importCase import ImportCase
from testapp.common.sendMsg import Sendqyapi



class CtManage(object):
    def __init__(self):
        self.datafac = DataIOFactory()
        self.swg = GetSwagerApi()
        self.sch = Schedulerplan()
        self.imp = ImportCase()
        self.qymsg = Sendqyapi()


    def getSwaggerIntoDatabase(self, url, name):
        """
        获取url swagger中的api-doc放到数据库，取名为 name(Swagger)
        """
        return self.swg.getSwaggerIntoDatabase(url, name)


    def add_job(self, id):
        """
        启动定时任务
        """
        try:
            self.sch.add_job(id)
            return True, '定时任务开启成功'
        except Exception as e:
            return False, str(e)

    def romove_job(self, id):
        """
        停止定时任务
        """
        try:
            self.sch.romove_job(id)
            return True, '定时任务关闭成功'
        except Exception as e:
            return False, str(e)


    def importCsae(self, file_obj):
        """
        导入用例
        """
        return self.imp.importCase(file_obj)

    def getImportRecord(self):
        """
        获取导入记录
        """
        return self.datafac.getImportRecord()

    def getImportFile(self, f_name):
        """
        获取导入记录文件
        """
        return True, self.imp.getImportFile(f_name)

    def getCaseModel(self):
        """
        下载用例模板
        """
        return True, self.imp.getCaseModel()

    def getHelpFile(self):
        """
        下载帮助文档
        """
        return True, self.imp.getHelpFile()

    def getEnv(self, data):
        """
        获取环境信息
        """
        return self.datafac.getEnv(data)


    def sentQymsg(self, **kwargs):
        """
        发送企业消息
        测试报告信息
        """
        return self.qymsg.sendqy_msg(**kwargs)


    def getExeRecord(self, data):
        """
        获取/查询执行记录
        """
        return self.datafac.getExeRecord(data)


    def getRecortCaseRepByNum(self,data):
        """
        获取用例报告列表
        """
        return self.datafac.getRecortCaseRepByNum(data)

    def getRecordInfo(self, data):
        """
        获取测试报告信息
        """
        return self.datafac.getPlanExRecbyNum(data)


    def updateCat(self, data):
        """
        更新树节点
        """
        return  self.datafac.updateCat(data)

    def updateScheStatus(self, data):
        """
        添加或移除测试计划定时任务
        params: data, 测试计划id，添加或删除标识
        """
        isOK, result = self.datafac.getSchedulerByjobid(data['id'], jobtype=2)
        if isOK:
            obj_schid = result[0]['id']
            if data['scheStatus'] == 1:
                return self.add_job(obj_schid)
            else:
                return self.romove_job(obj_schid)
        else:
            return isOK, result

    def updataPlanScheStatus(self, data):
        """
        更新测试计划定时任务状态
        """
        return self.datafac.updataPlanScheStatus(data)


    def add_env(self, data):
        """
        新增测试环境
        """
        return self.datafac.add_env(data)

    def get_data_conf(self, env_id):
        """
        通过环境id获取数据库配置
        """
        return self.datafac.get_data_conf(env_id)

    def get_data_conf_info(self, env_id, type, name):
        """
        通过环境id获取数据库配置
        """
        return self.datafac.get_data_conf_info(env_id, type, name)

    def updata_env(self, data):
        """
        更新环境信息
        """
        return self.datafac.updata_env(data)